package com.zhiyou100.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.entity.Worker;
import com.zhiyou100.service.WorkerService;
import com.zhiyou100.util.md5.Md5Util;
import com.zhiyou100.util.object.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * user 行为类
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-08-27 20:32
 */
@Controller
@RequestMapping("/user")
public class WorkerAction {
    private WorkerService service;

    @Autowired
    public void setService(WorkerService service) {
        this.service = service;
    }

    /**
     * 登陆,判断用户名和密码是否合理，获取🧂，判断是否存在此人，
     * 若存在，放入 session 中，跳转到 admin 中
     *
     * @param username 用户 id
     * @param password 用户 密码
     * @return 跳转的地址
     */
    @RequestMapping("/login.action")
    String login(String username, String password, HttpServletRequest request) {
        if (username == null || username.trim().length() == 0) {
            request.setAttribute("login_message", "id 为 空");
            return "login";
        }
        if (password == null || password.trim().length() == 0) {
            request.setAttribute("login_message", "密码 空");
            return "login";
        }

        // 获取 🧂
        final String key = request.getServletContext().getInitParameter("key");
        final Worker worker = service.selectOneByIdAndPwd(username, Md5Util.md5(password, key));
        if (worker == null) {
            // 没有查询到，设置错误信息，跳转到登陆页面
            request.setAttribute("login_message", "id 或者密码错误");
            return "login";
        }
        // 查询到，有此人,信息放入 session 中，重定向到 admin
        request.getSession().setAttribute("user", worker);
        return "redirect:/admin.jsp";
    }

    /**
     * 登出，清空 session，回到登陆页面
     *
     * @param request request
     * @return 登陆地址
     */
    @RequestMapping("/signOut.action")
    String signOut(HttpServletRequest request) {
        final HttpSession session = request.getSession();
        // 清空 session
        session.invalidate();
        return "redirect:/login.jsp";
    }


    @RequestMapping("/list.action")
    public String listHouses(Model model,
                             @RequestParam(defaultValue = "1",  value = "pageNo") Integer pageNo
    ) {
        // 每页显示的记录数
        int pageSize = 10;

        // 分页查询
        PageHelper.startPage(pageNo, pageSize);

        // 获取所有的信息
        final List<Worker> list = service.list();

        // 使用 PageInfo<Lessee> 结果进行封装

        final PageInfo<Worker> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo", pageInfo);

        return "view/worker/list";
    }

    @RequestMapping("/getOne.action")
    public String getOneByHid(Integer id, String method, Model model) {
        final Worker worker = service.selectOne(id);
        if (worker != null) {
            // 有，放入区域中，跳转 到 详情页面
            if (method == null || method.trim().length() == 0) {
                // 跳转到 view/worker/detail
                model.addAttribute("worker", worker);
                return "view/worker/detail";
            } else {
                // 跳转到更新界面
                model.addAttribute("worker", worker);
                return "view/worker/update";
            }

        } else {
            // 没有，返回列表
            return "view/worker/list";
        }
    }

    @RequestMapping("/insertOne.action")
    public String insertOne(Worker worker, HttpServletRequest request) {

        String id = "wid";
        String addTime = "wintime";
        if (ObjectUtil.onlyOneFiledIsNullAndTheFiledIsId(worker, id, addTime)) {
            // lid 和  laddtime 字段为空，合法
            // 设置时间
            worker.setWintime(new Date());
            // 密码加盐
            // 获取 🧂
            final String key = request.getServletContext().getInitParameter("key");

            // 加密之后设置回去
            worker.setWpwd(Md5Util.md5(worker.getWpwd(), key));
            service.insertOne(worker);
        }

        return "/view/worker/list";
    }

    @RequestMapping("/updateOne.action")
    public String updateOneByCid(Worker worker, HttpServletRequest request) {

        String addTime = "wintime";
        if (ObjectUtil.onlyOneFiledIsNullAndTheFiledIsId(worker, addTime)) {
            // laddtime 字段为空，合法,设置添加时间
            worker.setWintime(new Date());
            // 密码加盐
            // 获取 🧂
            final String key = request.getServletContext().getInitParameter("key");
            // 加密之后设置回去
            worker.setWpwd(Md5Util.md5(worker.getWpwd(), key));
            service.updateOne(worker);
        }

        return "/view/worker/list";
    }


    @RequestMapping("/deleteOne.action")
    public String deleteOneByHid(Integer id) {
        if (id != null) {
            service.deleteOne(id);
        }
        return "view/worker/list";
    }


}
